﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MoveTest
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string windowName);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(IntPtr hwndParent, string windowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;
        const int WM_MOVING = 0x0216;
        const short SWP_NOMOVE = 0X2;
        const short SWP_NOSIZE = 1;
        const short SWP_NOZORDER = 0X4;
        const int SWP_SHOWWINDOW = 0x0040;
        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;
        const int WM_CHAR = 0x0102;
        const int VK_SPACE = 0x20;

        static void Main(string[] args)
        {
            IntPtr wnd = FindWindow(IntPtr.Zero, "PowerPoint Slide Show - ["+ "Смешанная реальность – это просто.pptx" + "]");
            //IntPtr wnd1 = FindWindowEx(wnd, IntPtr.Zero, "paneClassDC", "PowerPoint Slide Show - [" + "Смешанная реальность – это просто.pptx" + "]");
            //var res = PostMessage(wnd, WM_KEYUP, VK_SPACE, 1);
            //var res1 = SendMessage(wnd, WM_CHAR, VK_SPACE, 1);
            //var res2 = PostMessage(wnd, WM_KEYUP, 0x41, 1);
            //var res3 = SendMessage(wnd, WM_CHAR, 0x41, 1);
            //SendMessage(wnd, 0x0416, 101, 101);

            SetForegroundWindow(wnd);
            SendKeys.SendWait(" ");

            SendMessage(wnd, WM_KEYUP, VK_SPACE, 1);
            //SendMessage(new IntPtr(0x000507FE), WM_KEYDOWN, VK_SPACE, 1);
            //SendMessage(new IntPtr(0x000507FE), WM_KEYDOWN, VK_SPACE, 1);
            //IntPtr wnd1 = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Slide Show", null);
            //IntPtr wnd = FindWindow(IntPtr.Zero, "WebCamWorkerWindow");
            //if (wnd != IntPtr.Zero)
            //{
            //    SetWindowPos(wnd, 0, -400, 0, 800, 600, SWP_NOZORDER | SWP_SHOWWINDOW);
                //SendMessage(wnd, WM_MOVING, 100, 100);
            //}
            // находим окно калькулятора, зная window class ("SciCalc")
            //IntPtr calcWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Calculator", null);
            //
            //// находим кнопку по имени окна ("1") и имени класса ("Button")
            //IntPtr button1 = FindWindowEx(calcWnd, IntPtr.Zero, "Button", "1");
            //// если нашли
            //if (button1 != IntPtr.Zero)
            //{
            //    // посылаем нажатие кнопки мыши, последний параметр
            //    // содержит координаты мыши (скопировано из Spy++)
            //    SendMessage(button1, WM_LBUTTONDOWN, 1, 0x150018);
            //    SendMessage(button1, WM_LBUTTONUP, 0, 0x150018);
            //}
        }
    }
}
